
/**
 * 游戏容器/层级管理类
 **/
class GameLayerManager extends eui.UILayer {
	// 实例
	private static _instance: GameLayerManager;

	// 弹窗层
	public popLayer = new eui.UILayer();
	// 游戏层
	public panelLayer = new eui.UILayer();
	// 
	public gameLayer = new egret.DisplayObjectContainer();

	// 可用来处理背景上下左右黑边
	private imgBg: eui.Image;
	private imgTop: eui.Image;
	private imgBottom: eui.Image;

	public static offsetX: number = 0;
	public static offsetY: number = 0;

	public static top: number = 0;
	public static bottom: number = 0;
	public static leftright: number = 0;

	// 适配机型
	public static phoneArray: any = ['vivox21', 'oppoa5'];

	public constructor() {
		super()
		this.init();
	}

	//游戏容器管理器单例
	public static instance(): GameLayerManager {
		if (!this._instance)
			this._instance = new GameLayerManager();
		return this._instance;
	}

	public init(): void {
		this.addChild(this.gameLayer);
		this.addChild(this.panelLayer);
		this.addChild(this.popLayer);

		this.popLayer.touchEnabled = false;
	}

	/** 适配处理 */
	public resetPos(): void {
		// TODO 不同小游戏平台刘海UI处理
		// 针对安卓机型适配
		let agent = window.navigator.userAgent;
		let isAndroid = agent.indexOf('Android') > -1 || agent.indexOf('Linux') > -1;
		if (isAndroid) {
			let phoneName = this.getPhoneName();
			phoneName = phoneName.replace(/\s*/g, "");
			phoneName = phoneName.toLowerCase();
			console.log("phoneName:" + phoneName);
			let index = this.arrayContains(GameLayerManager.phoneArray, phoneName);
			if (index != -1) { this.dealOtherUI(); }
		}

		console.log("适配数据:top=" + GameLayerManager.top + " // bottom="
			+ GameLayerManager.bottom + " // leftright=" + GameLayerManager.leftright);
		this.updateLayerUIPos();
	}

	private updateLayerUIPos(): void {
		GameLayerManager.offsetX = GameLayerManager.leftright;
		GameLayerManager.offsetY = GameLayerManager.top;
		this.panelLayer.left = this.panelLayer.right = GameLayerManager.leftright;
		this.popLayer.left = this.popLayer.right = GameLayerManager.leftright;

		this.panelLayer.top = GameLayerManager.top;
		this.panelLayer.bottom = GameLayerManager.bottom;
		this.popLayer.top = GameLayerManager.top;
		this.popLayer.bottom = GameLayerManager.bottom;
	}

	public static getGlobalPos(target: egret.DisplayObject, localX: number = 0, localY: number = 0, resultPoint: egret.Point = null): egret.Point {
		let pos = target.localToGlobal(localX, localY, resultPoint);
		pos.x -= GameLayerManager.offsetX;
		pos.y -= GameLayerManager.offsetY;
		return pos;
	}

	/** 最底层背景图 */
	public showImgBg(source): void {
		if (this.imgBg == null) {
			this.imgBg = new eui.Image();
			this.imgBg.top = this.imgBg.bottom = 0;
			this.imgBg.left = this.imgBg.right = 0;
			this.imgBg.fillMode = egret.BitmapFillMode.REPEAT;
			this.addChildAt(this.imgBg, 0);
		}
		this.imgBg.source = source;
	}

	// 非特殊统一处理(基本就是安卓机型)
	private dealOtherUI(): void {
		let width = egret.Capabilities.boundingClientWidth;
		let height = egret.Capabilities.boundingClientHeight;
		if (height / width >= 2) {
			GameLayerManager.top = 44;
		}
	}

	/** 获取手机型号 */
	private getPhoneName(): string {
		let total = window.navigator.userAgent;
		var model = "";
		var sss = total.split(";");
		var i = this.arrayContains(sss, "Build/");
		if (i > -1) { model = sss[i].substring(0, sss[i].indexOf("Build/")); }

		return model;
	}

	// 判断数组中是否包含某字符串
	public arrayContains(array: any, needle) {
		for (let i in array) {
			if (array[i].indexOf(needle) >= 0) return i;
		}
		return -1;
	}
}